package com.se.nused;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author hdu_huang
 * @since 2024/1/17 10:26
 */
public class DynamicProxy {

    public static interface IPerson extends Runnable{
        void tell(Object... obj);
    }

    public static class Person implements IPerson{
        @Override
        public void run() {
            System.out.println("run");
        }
        @Override
        public void tell(Object... obj) {
            System.out.println("tell obj");
        }
    }

    public static class Hander implements InvocationHandler {
        private final Object target;
        public Hander(Object target){
            this.target = target;
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("before");
            Object result = method.invoke(target, args);
            System.out.println("after");
            return result;
        }
    }
    public static void main(String[] args) throws Exception{

        Person person = new Person();

        IPerson proxy = (IPerson) Proxy.newProxyInstance(Person.class.getClassLoader(), new Class[]{IPerson.class}, new Hander(person));

        proxy.tell("123", "456");

        //java11 bug还在 java17修复
        Method method = proxy.getClass().getDeclaredMethod("tell", Object[].class);
        boolean varArgs = method.isVarArgs(); //true

        System.out.println("varArgs = " + varArgs);

    }
}
